package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.dao.JobMapper;
import com.example.shengyin.entity.Job;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.Seeker;
import com.example.shengyin.service.JobService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/job")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
//swagger
@Api("工作招聘")
//redis缓存用
public class JobController {
    @Autowired
    private JobService service;
    @Autowired
    private JobMapper jobMapper;

    /**
     * 查询岗位招聘列表
     */
    @ApiOperation("查询所有已发布岗位的名称、工作地址、岗位类型、岗位要求等所有信息")
    @GetMapping("/list")
    public Result list(@ApiParam(name = "page",  value = "页数" )  int page,
                       @ApiParam(name = "limit",value = "页条数")  int limit,
                       @ApiParam(name = "jobtitle", value = "岗位名称") @RequestParam(required = false) String jobtitle,
                       @ApiParam(name = "pos", value = "岗位地址") @RequestParam(required = false) String pos,
                       @ApiParam(name = "jobtype", value = "岗位类型") @RequestParam(required = false) String jobtype,
                       @ApiParam(name = "pt", value = "搜索岗位发布时间")  @RequestParam(required =
                               false) String pt) {

        Page page1 = service.jobList(page, limit, jobtitle, pos, jobtype, pt);
        // List<Job> list = service.jobList(page, limit, jobtitle, pos, jobtype, pt);
        //int count = service.countTotall(jobtitle, pos, jobtype, pt);
        if (page1.getCount() == 0 || page1.getData() == null)
            return Result.success(page, "没有数据");

        int pageTotal = page1.getPageTotal();
        System.out.println("pageTotal="+pageTotal);
        System.out.println(JSON.toJSONString(Result.success(page1)));
        return Result.success(page1);
    }

    @ApiOperation("查询某个已发布岗位名称、工作地址、发布时间、岗位要求等所有信息")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Integer id) {
        Job job = service.selectById(id);
        if (job == null)
            return Result.error("没有找到数据");
        return Result.success(job);
    }

    @ApiOperation("添加一个待招聘岗位,需要填写对应岗位信息")
    @PostMapping("/add")
    public Result insert(@Valid Job job, BindingResult bindingResult) {
        Timestamp d = new Timestamp(System.currentTimeMillis());
        job.setPosttime(d);
        int insert = service.insert(job);
        if (insert == 0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }

    @ApiOperation("对某个已发布岗位的信息进行更新")
    @PostMapping("/edit")
    public Result edit(Job job) {
        Timestamp d = new Timestamp(System.currentTimeMillis());
        job.setPosttime(d);
        int insert = service.update(job);
        if (insert == 0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @ApiOperation("删除某个已发布的岗位")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id") Integer id) {
        int delete = service.deleteById(id);
        if (delete == 0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @ApiOperation("删除多个已发布的岗位")
    @GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids") String ids) {
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if (delete == 0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }


    @ApiOperation("查找已发布的岗位对应应聘者投递的简历信息")
    @GetMapping("/selResume/{id}")
    public Result selResume(@PathVariable(value = "id") Integer id) {

        System.out.println("id="+id);
        Job job = service.selResumeByJobId(id);
        System.out.println("job="+job);
        if (job == null)
            return Result.error("没有找到");

        List<Seeker> jobSeeker = job.getJobSeeker();

        return Result.success(jobSeeker);
    }
}
